package com.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gym.pojo.Admin;
import com.gym.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    //模糊查询并分页查询
    @RequestMapping("/getAdminList")
    @ResponseBody
    public Map<String, Object> getAdminList(Integer page, Integer limit) {
        Page<Admin> adminPage = new Page<>(page, limit);
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户名模糊查询
//        queryWrapper.like(userName != null, UserInfo::getUserName, userName);
        Page<Admin> admin = adminService.page(adminPage, queryWrapper);
        List<Admin> adminList = admin.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", 0);
        map.put("data", adminList);
        //count是total
        map.put("count", admin.getTotal());
        return map;
    }

    //修改
    @RequestMapping("/editAdmin")
    @ResponseBody
    public Map<String, Object> editAdmin(Admin admin) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = adminService.updateById(admin);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }
}
